﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using Xoohoo.Infrastructure.FastReflectionLib;

namespace Xoohoo.Html
{
    public static class LabelExtensions
    {
        #region Static Constructor

        private static IMethodInvoker s_LabelHelperInvoker;
        static LabelExtensions()
        {
            MethodInfo labelHelperMethodInfo = typeof(System.Web.Mvc.Html.LabelExtensions).GetMethod("LabelHelper", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
            s_LabelHelperInvoker = new MethodInvoker(labelHelperMethodInfo);
        }

        #endregion

        #region Label

        public static MvcHtmlString LabelFor<TViewModel, TModel, TProperty>(this HtmlHelper<TViewModel> htmlHelper
            , TModel model
            , Expression<Func<TModel, TProperty>> expression)
        {
            return htmlHelper.LabelFor(model, expression, null);
        }
        public static MvcHtmlString LabelFor<TViewModel, TModel, TProperty>(this HtmlHelper<TViewModel> htmlHelper
            , TModel model, Expression<Func<TModel, TProperty>> expression
            , string labelText)
        {
            ViewDataDictionary<TModel> viewData = new ViewDataDictionary<TModel>(model);
            return (MvcHtmlString)s_LabelHelperInvoker.Invoke(
                null
                , htmlHelper
                , ModelMetadata.FromLambdaExpression(expression, viewData)
                , ExpressionHelper.GetExpressionText(expression)
                , labelText);
        }
        #endregion

    }
}
